package service;

import application.util.IOUtils;
import dao.inte.ConfigurationRepository;
import domain.MyConfiguration;
import domain.cms.CmsHomePage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import web.admin.config.DirectoryPathException;

import java.io.*;

@Transactional
public class ConfigurationServiceImpl implements ConfigurationService {
    @Autowired(required = true)
    private ILoggerService loggerService;
    private ConfigurationRepository configurationRepository;

    public ConfigurationServiceImpl(ConfigurationRepository configurationRepository) {
        if (configurationRepository == null) {
            throw new IllegalArgumentException("configurationRepository cannot be null");
        }
        this.configurationRepository = configurationRepository;
    }

    public ConfigurationServiceImpl() {
    }

    public void updateConfiguration(MyConfiguration newConfiguration) throws Exception {
        loggerService.log(ILoggerService.ActionType.CHANGE_SETTINGS);
        createFolders(newConfiguration);
        configurationRepository.save(newConfiguration);
    }

    public MyConfiguration getConfig() {
        return configurationRepository.getConfig();
    }

    public void updateCmsHomePage(CmsHomePage cmsHomePage) throws Exception {
        MyConfiguration configuration = configurationRepository.getConfig();
        configuration.setCmsHomePage(cmsHomePage);
        configurationRepository.save(configuration);
    }

    private void createFolders(MyConfiguration newConfiguration) {
        IOUtils.createFolder(newConfiguration.getImageDirectory());
        IOUtils.createFolder(newConfiguration.getTemplateDirectory());
        IOUtils.createFolder(newConfiguration.getXmlDirectory());
        IOUtils.createFolder(newConfiguration.getContentInvetoryDir());
    }

    public CmsHomePage getCmsHomePage() {
        return getConfig().getCmsHomePage();
    }

    public void saveTemplate(String templateStream,
                             String fileName) throws IOException, DirectoryPathException {

//        loggerService.log(ILoggerService.ActionType.CHANGE_SETTINGS);


        if (getConfig().getTemplateDirectory() == null) {
            throw new DirectoryPathException("Template directory is null");
        }


        //TODO templaty, vojtech se na to podiva :]
        //co s tim?
        // Create file
        // Create multiple directories
        FileWriter fstream = null;

        fstream = new FileWriter(getConfig().getTemplateDirectory() + fileName);

        BufferedWriter out = new BufferedWriter(fstream);
        out.write(templateStream);
        //Close the output stream
        out.close();


    }

    public String loadTemplate(String fileName) throws IOException, DirectoryPathException {
        if (getConfig().getTemplateDirectory() == null) {
            throw new DirectoryPathException("Template directory is null");
        }

        InputStream inStream = null;
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        try {
            inStream = new FileInputStream(new File(getConfig().getTemplateDirectory() + fileName));
        } catch (FileNotFoundException e) {
            saveTemplate("", fileName);
            inStream = new FileInputStream(new File(getConfig().getTemplateDirectory() + fileName));
        }
        IOUtils.copy(inStream, outStream);
        inStream.close();
        outStream.close();
//      outStream.toByteArray();
        return outStream.toString();
    }
}
